<?php

namespace backend\models;

/**
 * @author  shuang
 * @date    2016-12-24 19:10:43
 * @version V1.0
 * @desc    
 */
use common\models\User;
use Yii;
use yii\helpers\ArrayHelper;

class MY_User extends User {

    public static $froststatus = [
        0=>"正常",1=>"已冻结",2=>"已拉黑"
    ];
    public function getUserprofile() {
        return $this->hasOne(MY_UserProfile::className(), ['userid' => 'id']);
    }

    public function getUseraccount() {
        return $this->hasOne(MY_UserAccount::className(), ['userid' => 'id']);
    }
    public function getUseraccounthy() {
        return $this->hasOne(MY_UserAccountHy::className(), ['userid' => 'id']);
    }

    public function getGrade() {
        return $this->hasOne(MY_Grade::className(), ['id' => 'gradeid']);
    }

    /*
     * 商户总任务量
     */

    public function getTaskCount($userid) {
        $query = new \yii\db\Query();
        $query->from(\frontend\models\WB_Task::tableName());
        $query->select("count(userid) as num");
        $query->groupBy("userid");
        $query->andWhere("userid=:userid", [":userid" => $userid]);
        return $query->one();
    }

    /*
     * 商户、刷手注册统计
     * @params $begin_at
     * @params $end_at
     * @params $role
     */

    public static function getUserRegistCount() {
        $now = time();
        $query = new \yii\db\Query();
        $query->from(MY_User::tableName());
        $query->where("role=:role", [":role" => Yii::$app->request->get("role", 1)]);
        $query->andFilterWhere([">=", "created_at", strtotime(Yii::$app->request->get("begin_at", Yii::$app->formatter->asDate($now)))]);
        $query->andFilterWhere(["<=", "created_at", strtotime(Yii::$app->request->get("end", Yii::$app->formatter->asDate($now)))]);
        $query->select(["count(id) as num", "DATE_FORMAT(FROM_UNIXTIME(created_at),'%Y-%m-%d') AS date_stat"]);
        $query->groupBy(["DATE_FORMAT(FROM_UNIXTIME(created_at),'%Y-%m-%d')"]);
        $result = $query->all();
        return ["nums" => ArrayHelper::map($result, "num", "num"), "date" => ArrayHelper::map($result, "date_stat", "date_stat")];
    }

    /*
     * 查询商户列表
     */

    public static function getMerList() {
        $query = MY_User::find();
        $query->select('me_user.id,me_user.froststatus,me_user.created_at as regist_at,me_user.last_login_at,me_user.my_code,me_user.login_ip,me_user.login_area,grade.name as grade,userprofile.*,account.amount,account.rebate_amount');
        $query->leftJoin(['userprofile' => 'me_user_profile'], 'userprofile.userid = me_user.id');
        $query->leftJoin(['grade' => 'me_grade'], 'userprofile.gradeid = grade.id');
        $query->leftJoin(['account' => 'me_user_account'], 'account.userid = userprofile.userid');
        $query->leftJoin("(select userid,count(id) as taskcont from me_task group by userid) as usertask", "usertask.userid=me_user.id");
        $query->addSelect("usertask.taskcont");
        $query->andWhere("me_user.role=1");
        $query->andFilterWhere(["like", "userprofile.username", Yii::$app->request->get("search")]);
        $query->orderBy("me_user.created_at desc");
        $countQuery = clone $query;
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $totalCount = $countQuery->count();
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["total" => $totalCount, "data" => $res];
    }

    /*
     * 查询刷手列表
     */

    public static function getBrushList() {
        $query = MY_User::find();
        $query->select('me_user.id,me_user.froststatus,me_user.username,me_user.created_at as regist_at,me_user.last_login_at,me_user.my_code,me_user.login_ip,me_user.login_area,account.amount,account.offline_amount');
        $query->leftJoin(['account' => 'me_user_account_hy'], 'account.userid = me_user.id');
        $query->andWhere("me_user.role=2");
        $query->andFilterWhere(["like", "me_user.username", Yii::$app->request->post("search")]);
        $query->orderBy("me_user.created_at desc");
        $countQuery = clone $query;
        $offset = Yii::$app->request->post("offset");
        $limit = Yii::$app->request->post("limit");
        $totalCount = $countQuery->count();
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["total" => $totalCount, "data" => $res];
    }

    /*
     * 查看商户详情
     */

    public static function getDetail() {
        $userid = Yii::$app->request->get("id");
        $user = MY_User::findOne($userid);
        if (!$user instanceof MY_User) {
            throw new \yii\web\NotFoundHttpException;
        }
        return ["user" => $user];
    }

    /*
     * 刷手完成任务量
     * @params $userid
     */

    public static function getCompleteTaskCount($userid) {
        $query = new \yii\db\Query();
        $query->from(MY_UserTaskOperate::tableName());
        $query->andWhere("userid=:userid", [":userid" => $userid]);
        $query->andWhere("status=:status", [":status" => 1]);
        return $query->count();
    }

    /*
     * 刷手被投诉任务量
     * @params $userid
     */

    public static function getcomplaintTaskCount($userid) {
        $query = new \yii\db\Query();
        $query->from(MY_UserTaskOperate::tableName());
        $query->andWhere("userid=:userid", [":userid" => $userid]);
        $query->andWhere("status=:status", [":status" => 1]);
        $query->andWhere("is_complain > 0");
        return $query->count();
    }

    /*
     * 刷手主动隐藏任务量
     * @params $userid
     */

    public static function getHiddenTaskCount($userid) {
        $query = new \yii\db\Query();
        $query->from(\frontendm\models\WB_TaskHidden::tableName());
        $query->andWhere("userid=:userid", [":userid" => $userid]);
        return $query->count();
    }

    /*
     * 刷手邀请推广数量
     * @params $invite_code
     */

    public static function getInviteTaskCount($invite_code) {
        $query = new \yii\db\Query();
        $query->from(MY_UserProfile::tableName());
        $query->andWhere("invite_code=:invite_code", [":invite_code" => $invite_code]);
        return $query->count();
    }

}
